package com.fireline.beauty.cashier;

import java.math.BigDecimal;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.fireline.beauty.bean.HyxfxmVo;
import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.common.Assert;
import com.zgw.fireline.base.controls.DataCombo;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.widgets.ControlUtil;

/**
 * 添加套餐消费项目
 * */
public class TjTcxfDialog extends Dialog {

	protected HyxfxmVo result;
	protected Shell shell;
	private Spinner textCs;
	private Table table;
	/**
	 * @wbp.nonvisual location=17,542
	 */
	public final Dataset fwxm = new Dataset(
			"select a.tcbh,a.xmbh,a.cs,a.sycs,a.yhdj,a.zq,a.bz,b.xmmc,b.fwsj\r\n from hytc a left join fwxm b on a.xmbh=b.xmbh\r\n\twhere a.hybh=? and a.xmbh=?\r\n",
			SqliteJdbcImpl.INSTANCE);

	private CLabel lblNewLabel_1;
	private CLabel label_1;
	private CLabel label_3;
	private CLabel label_9;
	public String xmbh;
	// , cw, xmmc; // 项目编号、 床位
	private String hybh;
	// public int cs = 1, sycs;
	/**
	 * @wbp.nonvisual location=87,542
	 */
	private final Dataset cwszSet = new Dataset("select * from cwsz",
			SqliteJdbcImpl.INSTANCE);
	private DataCombo comboCw;
	private Text textKncs;
	private HyxfxmVo hyxfxm;// 会员消费内容

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 * @wbp.parser.constructor
	 */
	public TjTcxfDialog(Shell parent, HyxfxmVo hyxfxm, String hybh) {
		super(parent, SWT.NONE);
		this.hyxfxm = hyxfxm;
		this.xmbh = hyxfxm.xmbh;
		this.hybh = hybh;
	}

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public TjTcxfDialog(Shell parent, String xmbh, String hybh) {
		super(parent, SWT.NONE);
		this.xmbh = xmbh;
		this.hybh = hybh;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result SWT.OK |SWT.CANCEL
	 */
	public HyxfxmVo open() {
		createContents();
		ControlUtil.centerShell(shell);
		Display display = getParent().getDisplay();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		fwxm.setKeyColumns(new String[] {});
		cwszSet.setKeyColumns(new String[] {});
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(491, 449);
		shell.setText("添加护理消费");
		GridLayout gl_shell = new GridLayout(1, false);
		gl_shell.marginHeight = 10;
		shell.setLayout(gl_shell);

		Group group = new Group(shell, SWT.NONE);
		GridLayout gl_group = new GridLayout(4, false);
		gl_group.horizontalSpacing = 0;
		gl_group.marginWidth = 20;
		group.setLayout(gl_group);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));
		group.setText("项目信息");

		Label lblNewLabel = new Label(group, SWT.NONE);
		lblNewLabel.setText("项目编号：");

		lblNewLabel_1 = new CLabel(group, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true,
				false, 1, 1));
		lblNewLabel_1.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		lblNewLabel_1
				.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));

		Label label = new Label(group, SWT.NONE);
		label.setText("项目名称：");

		label_1 = new CLabel(group, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false,
				1, 1));
		label_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_1.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));

		Label label_2 = new Label(group, SWT.NONE);
		label_2.setText("优惠单价：");

		label_3 = new CLabel(group, SWT.NONE);
		label_3.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_3.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));

		Label label_8 = new Label(group, SWT.NONE);
		label_8.setText("服务时间：");

		label_9 = new CLabel(group, SWT.NONE);
		label_9.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_9.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));

		Group group_1 = new Group(shell, SWT.NONE);
		GridLayout gl_group_1 = new GridLayout(4, false);
		gl_group_1.horizontalSpacing = 0;
		gl_group_1.marginWidth = 20;
		group_1.setLayout(gl_group_1);
		group_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		group_1.setText("划卡次数");

		Label label_6 = new Label(group_1, SWT.NONE);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_6.setText("卡内剩余次数：");

		textKncs = new Text(group_1, SWT.READ_ONLY);
		textKncs.setText("12");
		textKncs.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		textKncs.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		textKncs.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label lblNewLabel_2 = new Label(group_1, SWT.NONE);
		lblNewLabel_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel_2.setText("  本次消费次数：");

		textCs = new Spinner(group_1, SWT.BORDER);
		textCs.setSelection(1);
		textCs.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		textCs.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));

		ControlUtil.setEnterToFocusMove(textCs);
		GridData gd_textCs = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1,
				1);
		gd_textCs.widthHint = 50;
		gd_textCs.minimumWidth = 70;
		textCs.setLayoutData(gd_textCs);
		group_1.setTabList(new Control[] { textCs });

		Group group_2 = new Group(shell, SWT.NONE);
		group_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_group_2 = new GridLayout(3, false);
		gl_group_2.horizontalSpacing = 0;
		group_2.setLayout(gl_group_2);
		group_2.setText("床位及服务员工");

		Label lblNewLabel_3 = new Label(group_2, SWT.NONE);
		GridData gd_lblNewLabel_3 = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_lblNewLabel_3.horizontalIndent = 15;
		lblNewLabel_3.setLayoutData(gd_lblNewLabel_3);
		lblNewLabel_3.setText("床位：");

		comboCw = new DataCombo(group_2, SWT.READ_ONLY, cwszSet);
		comboCw.setShowText("${ch}");
		GridData gd_comboCw = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_comboCw.widthHint = 57;
		comboCw.setLayoutData(gd_comboCw);
		ControlUtil.setEnterToFocusMove(comboCw);

		ToolBar toolBar = new ToolBar(group_2, SWT.FLAT | SWT.RIGHT);
		GridData gd_toolBar = new GridData(SWT.LEFT, SWT.CENTER, true, false,
				1, 1);
		gd_toolBar.horizontalIndent = 50;
		toolBar.setLayoutData(gd_toolBar);
		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.setText("添加");

		ToolItem toolItem = new ToolItem(toolBar, SWT.SEPARATOR);
		toolItem.setText("");

		ToolItem tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.setText("修改");

		ToolItem toolItem_1 = new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem tltmNewItem_2 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_2.setText("删除");

		table = new Table(group_2, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
		table.setHeaderVisible(true);

		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(76);
		tableColumn.setText("员工编号");

		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(74);
		tblclmnNewColumn.setText("员工姓名");

		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(76);
		tableColumn_1.setText("提成方式");

		TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
		tableColumn_2.setWidth(79);
		tableColumn_2.setText("提成比例");

		TableColumn tableColumn_3 = new TableColumn(table, SWT.NONE);
		tableColumn_3.setWidth(100);
		tableColumn_3.setText("提成金额");
		group_2.setTabList(new Control[] { comboCw, toolBar });

		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doOkPressed();
			}
		});
		btnNewButton_1.setLayoutData(new RowData(80, SWT.DEFAULT));
		btnNewButton_1.setText("确定");

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				result = null;
				shell.close();
			}
		});
		btnNewButton.setLayoutData(new RowData(80, SWT.DEFAULT));
		btnNewButton.setText("退出");
		setParams();
		createGUIAfter();
		setValues();
		initializeData();
		countSycs();
		initializeListener();
	}

	private void initializeListener() {
		textCs.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				countSycs();
			}
		});
	}

	// 计算当前卡内剩余次数
	protected void countSycs() {
		BigDecimal sycs = new BigDecimal(fwxm.getString("sycs"))
				.subtract(new BigDecimal(textCs.getSelection()));
		textKncs.setText(sycs.toString());
	}

	private void initializeData() {
		if (hyxfxm != null) {
			textCs.setSelection(hyxfxm.cs);
			comboCw.setText(hyxfxm.cw == null ? "" : hyxfxm.cw);
		}
	}

	protected void doOkPressed() {
		if (hyxfxm == null)
			hyxfxm = new HyxfxmVo();
		hyxfxm.cs = textCs.getSelection();
		hyxfxm.cw = comboCw.getText();
		hyxfxm.xmbh = fwxm.getString("xmbh");
		hyxfxm.xmmc = fwxm.getString("xmmc");
		hyxfxm.dqcs = Integer.parseInt(textKncs.getText());
		hyxfxm.setXmlx(3);
		result = hyxfxm;
		shell.close();
	}

	protected void setParams() {
		fwxm.setParam(0, hybh);
		fwxm.setParam(1, xmbh);
	}

	protected void setValues() {
		Assert.isTrue(fwxm.getCount() > 0, "服务项目数据集记录为0");
		lblNewLabel_1.setText(fwxm.getValue("xmbh", String.class, ""));
		label_1.setText(fwxm.getValue("xmmc", String.class, ""));
		label_9.setText(fwxm.getValue("fwsj", String.class, ""));
		label_3.setText(fwxm.getValue("yhdj", String.class, ""));
		textKncs.setText(fwxm.getValue("sycs", String.class, ""));
	}

	protected void createGUIAfter() {
		cwszSet.update();
		fwxm.update();
		fwxm.setSelected(0);
	}
}
